package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"vipshop/model/mysql"
	"vipshop/vipshoprpc/internal/svc"
	"vipshop/vipshoprpc/vipshoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpDataLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpDataLogic {
	return &UserUpDataLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpDataLogic) UserUpData(in *vipshoprpc.UserUpDataRequest) (*vipshoprpc.UserUpDataResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("该账号不存在不允许填充个人信息")
	}
	newuser := mysql.User{
		Model:       gorm.Model{ID: user.ID},
		Age:         in.Age,
		AtivePlace:  in.AtivePlace,
		Email:       in.Email,
		HeaderImage: in.HeaderImage,
	}
	err := mysql.DB.Debug().Updates(&newuser).Error
	if err != nil {
		return nil, fmt.Errorf("修改失败")
	}
	return &vipshoprpc.UserUpDataResponse{
		Id: int64(newuser.ID),
	}, nil
}
